﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.sim.*;
    import Display.*;
    import Display.Toaster.*;
    import Classes.PopulationTriggers.*;
    import com.adobe.utils.*;

    public class PopulationMilestoneManager implements IPopulationStateObserver {

        public static const POPULATION_MILESTONE_NOTIFICATION_ACTIVATION_KEY:String = "populationMilestoneNotification";

        private var m_lastPopulation:int;
        protected var m_populationTriggers:Dictionary;

        public function PopulationMilestoneManager(){
            Global.world.citySim.addObserver(this);
            if (!Global.isVisiting()){
                this.m_lastPopulation = Global.world.citySim.getPopulation();
                this.loadPopulationTriggers();
            };
        }
        protected function loadPopulationTriggers():void{
            var _local2:XML;
            var _local3:IPopulationTrigger;
            var _local4:int;
            this.m_populationTriggers = new Dictionary();
            var _local1:XMLList = Global.gameSettings.getPopulationTriggersXML();
            for each (_local2 in _local1) {
                if ((((String(_local2.@experiment).length > 0)) && ((String(_local2.@expectedVariant).length > 0)))){
                    //unresolved if
                } else {
                    if ((((String(_local2.@className).length <= 0)) || ((String(_local2.@requiredPopulation).length <= 0)))){
                    } else {
                        _local3 = PopulationTriggerFactory.createPopulationTrigger(_local2);
                        if (!_local3){
                        } else {
                            _local4 = parseInt(_local2.@requiredPopulation);
                            if (!this.m_populationTriggers[_local4]){
                                this.m_populationTriggers[_local4] = new Array();
                            };
                            (this.m_populationTriggers[_local4] as Array).push(_local3);
                        };
                    };
                };
            };
        }
        public function onPopulationInit(_arg1:int, _arg2:int, _arg3:int):void{
        }
        public function onPopulationChanged(_arg1:int, _arg2:int, _arg3:int, _arg4:int):void{
            var populationDelta:* = 0;
            var currTime:* = NaN;
            var lastNotificationTime:* = 0;
            var milestoneItems:* = null;
            var thresholdPassingItems:* = null;
            var triggerIndices:* = null;
            var index:* = 0;
            var nextMilestoneThresholdItems:* = null;
            var nextMilestoneThresholdItem:* = null;
            var currItem:* = null;
            var toaster:* = null;
            var lastThresholdPassingItem:* = null;
            var currThresholdItem:* = null;
            var strButtonText:* = null;
            var strDialogTitle:* = null;
            var strDialogText:* = null;
            var strDialogIcon:* = null;
            var nextThresholdPassingItems:* = null;
            var dlg:* = null;
            var nextThresholdPassingItem:* = null;
            var currNextThresholdItem:* = null;
            var trigger:* = null;
            var newRaw:* = _arg1;
            var deltaRaw:* = _arg2;
            var newScaled:* = _arg3;
            var newTotal:* = _arg4;
            if (!Global.isVisiting()){
                populationDelta = (newRaw - this.m_lastPopulation);
                if (populationDelta > 0){
                    currTime = (GlobalEngine.getTimer() / 1000);
                    lastNotificationTime = Global.player.getLastActivationTime(POPULATION_MILESTONE_NOTIFICATION_ACTIVATION_KEY);
                    milestoneItems = Global.gameSettings.getItems().(attribute("isPopulationMilestoneTarget") == "true");
                    if ((currTime - lastNotificationTime) > Global.gameSettings.getNumber("populationMilestoneNoticeInterval", 0)){
                        nextMilestoneThresholdItems = milestoneItems.(((child("requiredPopulation") > newRaw)) && ((newRaw >= (child("requiredPopulation") * Global.gameSettings.getNumber("populationMilestoneNoticeRatio", 0.5)))));
                        if (nextMilestoneThresholdItems.length() > 0){
                            nextMilestoneThresholdItem = nextMilestoneThresholdItems[0];
                            for each (currItem in nextMilestoneThresholdItems) {
                                if (parseInt(currItem.child("requiredPopulation")) < parseInt(nextMilestoneThresholdItem.child("requiredPopulation"))){
                                    nextMilestoneThresholdItem = currItem;
                                };
                            };
                            toaster = new ItemToaster(ZLoc.t("Main", "ItemToasterGrowingTitle"), ZLoc.t("Main", "ItemToasterGrowingBody", {
                                item:Global.gameSettings.getItemFriendlyName(nextMilestoneThresholdItem.attribute("name")),
                                population:((nextMilestoneThresholdItem.child("requiredPopulation") - newRaw) * Global.gameSettings.getNumber("populationScale", 1))
                            }), Global.gameSettings.getImageByName(nextMilestoneThresholdItem.attribute("name"), "icon"));
                            Global.ui.toaster.show(toaster);
                            Global.player.setLastActivationTime(POPULATION_MILESTONE_NOTIFICATION_ACTIVATION_KEY, currTime);
                        };
                    };
                    thresholdPassingItems = milestoneItems.(((child("requiredPopulation") > m_lastPopulation)) && ((child("requiredPopulation") <= newRaw)));
                    if (thresholdPassingItems.length() > 0){
                        lastThresholdPassingItem = thresholdPassingItems[0];
                        for each (currThresholdItem in thresholdPassingItems) {
                            if (parseInt(currThresholdItem.child("requiredPopulation")) > parseInt(lastThresholdPassingItem.child("requiredPopulation"))){
                                lastThresholdPassingItem = currThresholdItem;
                            };
                        };
                        strButtonText = "OkButton";
                        strDialogTitle = ZLoc.t("Dialogs", "PopulationMilestone_title", {population:newScaled});
                        strDialogText = ZLoc.t("Dialogs", "PopulationMilestone_message_end", {population:newScaled});
                        strDialogIcon = "assets/dialogs/populationMilestone_celebrate.png";
                        nextThresholdPassingItems = milestoneItems.(child("requiredPopulation") > newRaw);
                        if (nextThresholdPassingItems.length() > 0){
                            nextThresholdPassingItem = nextThresholdPassingItems[0];
                            for each (currNextThresholdItem in nextThresholdPassingItems) {
                                if (parseInt(currNextThresholdItem.child("requiredPopulation")) < parseInt(nextThresholdPassingItem.child("requiredPopulation"))){
                                    nextThresholdPassingItem = currNextThresholdItem;
                                };
                            };
                            strDialogText = ZLoc.t("Dialogs", "PopulationMilestone_message", {
                                item:Global.gameSettings.getItemFriendlyName(nextThresholdPassingItem.attribute("name")),
                                population:(nextThresholdPassingItem.child("requiredPopulation") * Global.gameSettings.getNumber("populationScale", 1)),
                                cityName:Global.player.cityName
                            });
                        };
                        dlg = new GenericPictureDialog(strDialogText, strDialogTitle, GenericDialogView.TYPE_OK, null, strDialogTitle, "", true, 0, strButtonText, strDialogIcon);
                        UI.displayPopup(dlg);
                    };
                    triggerIndices = DictionaryUtil.getKeys(this.m_populationTriggers);
                    triggerIndices.sort(Array.NUMERIC);
                    for each (index in triggerIndices) {
                        if (index <= this.m_lastPopulation){
                        } else {
                            if (index > newRaw){
                                break;
                            };
                            for each (trigger in this.m_populationTriggers[index]) {
                                if (trigger.hasTriggered()){
                                } else {
                                    trigger.trigger();
                                };
                            };
                        };
                    };
                };
                this.m_lastPopulation = Global.world.citySim.getPopulation();
            };
        }
        public function onPotentialPopulationChanged(_arg1:int, _arg2:int):void{
        }
        public function onPopulationCapChanged(_arg1:int):void{
        }

    }
}//package Classes 
